package com.msh.frame.client.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.msh.frame.client.model.ApplicationServer;
import java.util.Map;
import org.springframework.util.StringUtils;

/**
 * 本地开发工具类
 */
public class LocalDevelopmentUtil {
  public final static String HEADER_PATH_ROUTE = "path-route";
  public final static String HEADER_SERVER_ROUTE = "server-route";

  public static ApplicationServer getPathRoute(String pathRouter, String path){
    // 路径转发
    if(StringUtils.hasText(pathRouter)){
      Map<String, ApplicationServer> map = JSON
          .parseObject(pathRouter, TypeReference_ROUTER_SERVER_MAP);
      ApplicationServer routerServer = map.get(path);
      if(null != routerServer){
        return routerServer;
      }
    }
    return null;
  }

  public static ApplicationServer getServerRoute(String serverRouter, String applicationName){
    // 服务转发
    if(StringUtils.hasText(serverRouter)){
      Map<String, ApplicationServer> map = JSON
          .parseObject(serverRouter, TypeReference_ROUTER_SERVER_MAP);
      ApplicationServer routerServer = map.get(applicationName);
      if(null != routerServer){
        return routerServer;
      }
    }
    return null;
  }

  private static TypeReference<Map<String, ApplicationServer>> TypeReference_ROUTER_SERVER_MAP = new TypeReference<Map<String, ApplicationServer>>(){};
}
